package com.bootdo.company.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.company.domain.CompanyPublishPostDO;
import com.bootdo.system.controller.StatsController;
import com.bootdo.system.domain.UserDO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.bootdo.company.domain.CompanyDO;
import com.bootdo.company.service.CompanyService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 公司基本信息表
 * 
 * @author wudongliang
 * @email 460030103@qq.com
 * @date 2020-02-18 10:46:45
 */

@Controller
@RequestMapping("/company/company")
public class CompanyController {
	@Autowired
	private CompanyService companyService;
	
	@GetMapping()
	@RequiresPermissions("company:company:company")
	String Company(){
	    return "company/company/company";
	}
	
	@ResponseBody
	@GetMapping("/list")
//	@RequiresPermissions("company:company:company")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<CompanyDO> companyList = companyService.list(query);
		int total = companyService.count(query);
		PageUtils pageUtils = new PageUtils(companyList, total);
		return pageUtils;
	}

	@ResponseBody
	@GetMapping("/lists")
	public R lists(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		PageUtils pageUtils = null;
		try{
			List<CompanyDO> companyList = companyService.list(query);
			int total = companyService.count(query);
			 pageUtils = new PageUtils(companyList, total);

		}catch (Exception e)
		{
			return  R.error(101,"数据查询失败");
		}
		return R.ok().put("data",pageUtils);
	}


	@ResponseBody
	@GetMapping("/companypostlist")
	public R companyPostLists(@RequestParam Map<String, Object> params){
		//查询列表数据
//		Query query = new Query(params);
		PageUtils pageUtils = null;
		List<CompanyPublishPostDO> companyList;
		try{
			companyList = companyService.companyPostLists();
//			int total = companyService.count(query);
//			pageUtils = new PageUtils(companyList, total);
			StatsController.addVisitTotal();
		}catch (Exception e)
		{
			return  R.error(101,"数据查询失败");
		}
		return R.ok().put("data",companyList);
	}




	@GetMapping("/add")
	@RequiresPermissions("company:company:add")
	String add(){
	    return "company/company/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("company:company:edit")
	String edit(@PathVariable("id") Long id,Model model){
		CompanyDO company = companyService.get(id);
		model.addAttribute("company", company);
	    return "company/company/edit";
	}

	@GetMapping("/edit")
	@RequiresPermissions("company:company:edit")
	String edit(Model model){
		Subject subject = SecurityUtils.getSubject();
		UserDO user = (UserDO)subject.getPrincipal();

		CompanyDO company = companyService.getByUserId(user.getUserId());
		if(company == null ){
			company= new CompanyDO();
		}
		model.addAttribute("company", company);
		return "company/company/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("company:company:add")
	public R save( CompanyDO company){
		Subject subject = SecurityUtils.getSubject();
		UserDO user = (UserDO)subject.getPrincipal();
		if(user == null){
			return R.error("用户登录过期");
		}
		company.setUserId(user.getUserId());
		if(companyService.save(company)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("company:company:edit")
	public R update( CompanyDO company){
		if(company==null){
			return R.error();
		}
		if(company.getId()==null){
			Subject subject = SecurityUtils.getSubject();
			UserDO user = (UserDO)subject.getPrincipal();
			if(user == null){
				return R.error("用户登录过期");
			}
			company.setUserId(user.getUserId());
			if(companyService.save(company)>0){
				return R.ok();
			}
		}
		companyService.update(company);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("company:company:remove")
	public R remove( Long id){
		if(companyService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("company:company:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		companyService.batchRemove(ids);
		return R.ok();
	}

	@PostMapping("/get")
	@ResponseBody
	R get(@RequestBody Map<String, Object> map) {
		Map<String,Object> data = companyService.getById(Long.valueOf(map.get("id")+""));
		return R.ok().put("data",data);
	}
}
